package scripts;
import bot.ScriptMethods;
import javax.swing.JOptionPane;

public class flaxBankPower extends ScriptMethods implements Runnable {

	public Thread scriptThread;
	public boolean scriptRunning = false;

	public void start() {
		System.gc();
		getAccountSettings();

		scriptRunning = true;
		scriptThread = new Thread(this);
		scriptThread.start();
	}

	public void stop() {
		System.gc();
		scriptRunning = false;
	}

	public void run() {
		
		Object[] options = { "Bank", "Drop" };
		
		String bankDrop = (String) JOptionPane.showInputDialog(null, "", "Will we bank or drop?", JOptionPane.PLAIN_MESSAGE, null, options, options[1]);
		while (scriptRunning) {
			try {
				handleEvents();
				
				if(playerInBounds(478, 481, 490, 492)){
					if(getInventorySize() < 30 && !invContains(676)){
						int[] flax = getObjectById(313);
						atObject2(flax[1], flax[2]);
						wait(600);
					} else {
						walkToWheel();
					}
				}
				
				if(playerInBounds(522,1406,525,1411) && !isSleeping()){
					if(invContains(675)){
						int[] spinningwheel = getObjectById(121);
						if(spinningwheel[0] != -1 && getFatigue() <= 80){
							useItemOnObject(spinningwheel[1], spinningwheel[2], 675);
							wait(600);
						}	
					} else {
						if(bankDrop.equals("Drop")){
							if(invContains(676)){
								dropItem(676);
								wait(600);
							} else {
								walkToFlax();
							}
						} else {
							walkToBank();
						}
					}
				}
				
				if(playerInBounds(498, 447, 504, 453)){
					if(bankDrop.contains("Bank") && invContains(676)){
							doBank(676);
						} else {
							walkToFlax();
					}
				}
				
				wait(random(50, 100));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	public void walkToFlax() throws Exception {
		while(!playerInBounds(478, 481, 490, 492)){
			handleEvents();
			if(playerInBounds(498, 447, 504, 453)){
				int[] bankDoor = getObjectById(64);
				if (bankDoor[0] != -1) {
					atObject(bankDoor[1], bankDoor[2]);
					wait(random(1000, 1200));
			    } 
				walkTo(500, 454, false);			      
			}
			
	  		if (ourPlayerX() <= 501 && ourPlayerY() >= 454 &&
	  			ourPlayerX() >= 498 && ourPlayerY() <= 462) {
	  	  			walkTo(501, 463, false);
	  		}
	  		
	  		if(ourPlayerX() == 501 && ourPlayerY()== 463){
	  			walkTo(501, 464, false);
	  		}
	  		
	  		if (ourPlayerX() <= 501 && ourPlayerY() >= 462 && 
	  			ourPlayerX() >= 491 && ourPlayerY() <= 485) { 
	  	  			walkTo(490, 485, false);
	  		}
	  			  		 		
			if(playerInBounds(522,1406,525,1411)){
				int[] ladderD = getObjectById(6);
				if (ladderD[0] != -1) { 
					atObject(ladderD[1], ladderD[2]);
					wait(2000);
				}
			}
			
	  		if (ourPlayerX() <= 526 && ourPlayerY() >= 461 &&
	  			    ourPlayerX() >= 491 && ourPlayerY() <= 485) {
	  	  				walkTo(490, 485, false);
	  		}
			wait(600);
		}
	}
	
	public void walkToBank() throws Exception {
		while(!playerInBounds(498, 447, 504, 453)){
			handleEvents();
			if(playerInBounds(522,1406,525,1411)){
				int[] ladderD = getObjectById(6);
				if (ladderD[0] != -1) { 
					atObject(ladderD[1], ladderD[2]);
					wait(1500);
				}	
			}
			
			if (ourPlayerX() >= 502 && ourPlayerY() >= 455 &&
				ourPlayerX() <= 525 && ourPlayerY() <= 467) {
					walkTo(501,454, false);
			}
			
			if (ourPlayerX() >= 500 && ourPlayerY() >= 454 &&
				ourPlayerX() <= 502 && ourPlayerY() <= 455) {
				int[] bankDoor = getObjectById(64);
					if (bankDoor[0] != -1) {
						atObject(bankDoor[1], bankDoor[2]);
						wait(random(1000, 1200));
					} 
				walkTo(501, 452, false);
			}	
		wait(600);	
		}
	}
	
	public void walkToWheel() throws Exception {
		while(!playerInBounds(522,1406,525,1411)){
			handleEvents();
			if (ourPlayerX() <= 507 && ourPlayerY() >= 476 &&
				ourPlayerX() >= 478 && ourPlayerY() <= 492) {
					handleEvents();
					walkTo(508,475,false);			
			}
			
			if (ourPlayerX() <= 524 && ourPlayerY() >= 463 &&
				ourPlayerX() >= 507 && ourPlayerY() <= 476) {
					handleEvents();
					walkTo(525,463,false);
			}

			if(ourPlayerX() == 525 && ourPlayerY() == 463){
				int[] ladderD = getObjectById(5);
				if (ladderD[0] != -1) {
					atObject(ladderD[1], ladderD[2]);
					wait(1500);
				}
			}
		wait(600);
		}
	}

	public void handleEvents() throws Exception {
		if (welcomeBoxOpen())
			setWelcomeBox();
		if (!isLoggedIn())
			doLogin();
		if (getFatigue() > 80 && !isSleeping()) {
			if (getFatigue() > 80 && !isSleeping()) {
				int[] bed = getObjectById(14);
				int[] bed2 = getObjectById(15);
				if (bed[0] != -1) {
					atObject(bed[1], bed[2]);
				} else if (bed2[0] != -1) {
					atObject(bed2[1], bed2[2]);
				}
				wait(1000);
			}
		}
		if (getFatigueSleeping() < 10 && isSleeping())
			doSleeping();
		if (getFightMode() != fightMode)
			setFightMode();
	}
	
	public boolean playerInBounds(int x1, int y1, int x2, int y2) throws Exception {
		return ourPlayerX() >= x1 && ourPlayerX() <= x2 && ourPlayerY() >= y1 && ourPlayerY() <= y2;
	}
	
	public void doBank(int deposit) {
		try{ 
			handleEvents();
				while(!bankOpen()){
					handleEvents();
					openBank();
					wait(1200);
				}
				
				if(bankOpen()){
						if(invContains(deposit)){
							depositAll(deposit);
							wait(600);
						}
			}
				closeBank();
				wait(600);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
}